package user;

import book.Library;
import constant.Constant;
import utils.NoUserPermissions;

public class ProxyUser {
    private User realUser;

    private Library library = Library.getInstance();

    public ProxyUser(User user){
        this.realUser = user;
    }

    public User getRealUser() {
        return realUser;
    }

    public int display(){
        return realUser.display();
    }

    public void checkAdminUserPermission(String msg){
        if(!(this.realUser instanceof AdminUser)){
            throw new NoUserPermissions(msg);
        }
    }

    public void checkNormalUserPermission(String msg){
        if(!(this.realUser instanceof NormalUser)){
            throw new NoUserPermissions(msg);
        }
    }

    public void operation(int choice){
        if (this.realUser instanceof AdminUser){
            // 管理员用户的方法
            switch (choice){
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.dispalyBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    BorrowedCount();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInvertoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBook();
                    break;

            }
        }else if(this.realUser instanceof NormalUser){
            // 普通用户的方法
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.dispalyBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.BORROW_BOOK:
                    borrowedBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistoryBook();
                    break;
            }

        }

    }

    // 管理员用户的功能
    private void checkAndRemoveOldBook() {
        checkAdminUserPermission("普通用户没有权限检查超过一年未下架的书籍!!!");
        System.out.println("代理类的checkAndRemoveOldBook执行了");
        ((AdminUser)realUser).checkAndRemoveOldBook();
    }

    private void checkInvertoryStatus() {
        checkAdminUserPermission("普通用户没有权限查看库存状态");
        System.out.println("代理类的checkInvertoryStatus执行了");
        ((AdminUser)realUser).checkInvertoryStatus();
    }

    private void generateBook() {
        checkAdminUserPermission("普通用户没有权限查看受欢迎的前k本书");
        System.out.println("代理类的generateBook执行了");
        ((AdminUser)realUser).generateBook();
    }

    private void BorrowedCount() {
        checkAdminUserPermission("普通用户没有权限统计书籍的借阅次数");
        System.out.println("代理类的BorrowedCount执行了");
        ((AdminUser)realUser).BorrowedCount();
    }

    private void removeBook() {
        checkAdminUserPermission("普通用户没有权限移除书籍!!!");
        System.out.println("代理类的removeBook执行了");
        ((AdminUser)realUser).removeBook();
    }

    private void updateBook() {
        checkAdminUserPermission("普通用户没有权限修改书籍!!!");
        System.out.println("代理类的updateBook执行了");
        ((AdminUser)realUser).updateBook();
    }

    private void addBook() {
        checkAdminUserPermission("普通用户没有权限添加书籍!!!");
        System.out.println("代理类的addBook执行了");
        ((AdminUser)realUser).addBook();
    }

    // 普通用户的功能

    private void viewBorrowHistoryBook() {
        checkNormalUserPermission("管理员用户请以普通用户的身份查看当前个人的借阅情况!");
        System.out.println("代理类的viewBorrowHistoryBook执行了");
        ((NormalUser)realUser).viewBorrowHistoryBook();
    }

    private void returnBook() {
        checkNormalUserPermission("管理员用户请以普通用户的身份归还书籍!!!");
        System.out.println("代理类的returnBook执行了");
        ((NormalUser)realUser).returnBook();
    }

    private void borrowedBook() {
        checkNormalUserPermission("管理员用户请以普通用户的身份借阅书籍!!!");
        System.out.println("代理类的borrowedBook执行了");
        ((NormalUser)realUser).borrowedBook();
    }

}
